<template>
  <div>
    <!-- 文物修复详情表 -->
    <div class="crfixifo">
      <span>修复详情单</span>
      <van-field
        v-model="crNo"
        :value="crNo"
        name="文物编号"
        label="文物编号"
        placeholder="文物编号"
        required="true"
        disabled
      />
      <van-field
        v-model="fcname"
        name="修复方姓名"
        @change="changefcName"
        @blur="nameblur"
        label="修复方姓名"
        placeholder="修复方姓名"
        :error-message="namemessage"
        required="true"
      />
      <van-field
        v-model="fSDate"
        name="修复起始日期"
        label="修复起始日期"
        placeholder="修复起始日期"
        @change="changefSDate"
        @blur="Dateblur"
        :error-message="Datemessage"
        required="true"
      />
      <van-field
        v-model="fEDate"
        @change="changefEDate"
        @blur="fEDateblur"
        :error-message="fEDatemessage"
        name="修复归还日期"
        label="修复归还日期"
        placeholder="修复归还日期"
        required="true"
      />
    </div>
    <!-- 文物操作记录表 -->
    <div class="Cropifo">
      <span>操作记录单</span>
      <van-field
        v-model="crNo"
        name="文物编号"
        label="文物编号"
        @change="changecrNo"
        required="true"
        placeholder="文物编号"
        disabled
      />
      <van-field
        v-model="eNo"
        name="工号"
        label="工号"
        placeholder="工号"
        @change="changeeNo"
        @blur="eNoblur"
        :error-message="eNomessage"
        required="true"
        disabled
      />
      <van-field
        v-model="opType"
        name="操作类型"
        label="操作类型"
        placeholder="操作类型"
        @change="changeop"
        @blur="opblur"
        :error-message="opmessage"
        required="true"
        disabled
      />
      <van-field
        v-model="opDate"
        name="操作时间"
        label="操作时间"
        placeholder="操作时间"
        @change="changeopDate"
        @blur="opDateblur"
        :error-message="opDatemessage"
        required="true"
      />
    </div>
    <!-- 修复联系人信息 -->
    <div class="Lendcontact">
      <span>修复联系人单</span>
      <van-field
        v-model="fcname"
        name="姓名"
        label="姓名"
        placeholder="姓名"
        @change="changefeName"
        @blur="feNameblur"
        :error-message="feNamemessage"
        required="true"
      />
      <van-field
        v-model="fcremark"
        name="备注"
        label="备注"
        placeholder="备注"
        @change="fcRemarkschange"
      />
    </div>
    <div style="margin: 16px;">
      <van-button round block type="info" native-type="submit" @click="submit">提交</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 验证失败提醒
      namemessage: "",
      Datemessage: "",
      fEDatemessage: "",
      eNomessage: "",
      opmessage: "",
      opDatemessage: "",
      feNamemessage: "",
      // 文物编号
      crNo: "",
      // 修复人姓名
      fcname: "",
      // 工号
      eNo: "",
      // 操作类型
      opType: "修复",
      // 借出时间
      fSDate: "",
      // 归还时间
      fEDate: "",
      // 操作时间
      opDate: "",
      // 修复人信息备注
      fcremark: ""
    };
  },
  methods: {
    // 名字改变时触发
    changefcName(event) {
      this.fcname = event.mp.detail.value;
      this.namemessage = "";
    },
    // 名字验证
    nameblur(event) {
      const name = event.mp.detail.value;
      let message = "";
      if (name.length === 0) {
        message = "名字不能为空";
        this.namemessage = message;
        return false;
      }
      return true;
    },
    // 修复起始日期改变时触发
    changefSDate(event) {
      this.fSDate = event.mp.detail.value;
      this.Datemessage = "";
    },
    // 起始日期验证
    Dateblur(event) {
      const data = event.mp.detail.value;
      let message = "";
      if (data.length === 0) {
        message = "时间不能为空";
        this.Datemessage = message;
        return false;
      }
      var regex = /((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
      if (!regex.exec(data)) {
        message = "格式 yyyy-MM-dd HH:mm:ss";
        this.Datemessage = message;
        return false;
      }
      console.log("时间输入成功");
      return true;
    },
    // 归还改变时触发
    changefEDate(event) {
      this.fEDate = event.mp.detail.value;
      this.fEDatemessage = "";
    },
    // 归还日期验证
    fEDateblur(event) {
      const data = event.mp.detail.value;
      let message = "";
      if (data.length === 0) {
        message = "时间不能为空";
        this.fEDatemessage = message;
        return false;
      }
      var regex = /((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
      if (!regex.exec(data)) {
        message = "格式yyyy-MM-dd HH:mm:ss";
        this.fEDatemessage = message;
        return false;
      }
      return true;
    },
    // 工号改变时触发的事件
    changeeNo(event) {
      this.eNo = event.mp.detail.value;
      this.eNomessage = "";
    },
    // 工号校验
    eNoblur(event) {
      const no = event.mp.detail.value;
      console.log(no);

      let message = "";
      if (no.length === 0) {
        message = "工号不能为空";
        this.eNomessage = message;
        return false;
      }
      var num = /^\d*$/; //全数字
      if (!num.exec(no)) {
        //必须全为数字;
        message = "工号必须全为数字";
        this.eNomessage = message;
        return false;
      }
      return true;
    },
    // 操作类型改变时触发的事件
    changeop(event) {
      this.opType = event.mp.detail.value;
      this.opmessage = "";
    },
    // 操作类型校验
    opblur(event) {
      const no = event.mp.detail.value;
      console.log(no);
      let message = "";
      if (no.length === 0) {
        message = "操作类型不能为空";
        this.opmessage = message;
        return false;
      }
      return true;
    },
    // 操作时间改变时触发
    changeopDate(event) {
      this.opDate = event.mp.detail.value;
      this.opDatemessage = "";
    },
    // 操作时间验证
    opDateblur(event) {
      const data = event.mp.detail.value;
      let message = "";
      if (data.length === 0) {
        message = "时间不能为空";
        this.opDatemessage = message;
        return false;
      }
      var regex = /((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
      if (!regex.exec(data)) {
        message = "格式yyyy-MM-dd HH:mm:ss";
        this.opDatemessage = message;
        return false;
      }
      return true;
    },
    // 修复人改变时触发
    changefeName(event) {
      this.fCname = event.mp.detail.value;
      this.feNamemessage = "";
    },
    // 外借验证
    feNameblur(event) {
      const name = event.mp.detail.value;
      let message = "";
      if (name.length === 0) {
        message = "名字不能为空";
        this.feNamemessage = message;
        return false;
      }
      return true;
    },
    fcRemarkschange(event) {
      console.log(event);
      console.log(event.mp.detail);
      this.fcremark = event.mp.detail;
    },
    // 提交表单
    async submit() {
      if (
        this.namemessage.length == 0 &&
        this.Datemessage.length == 0 &&
        this.fEDatemessage.length == 0 &&
        this.eNomessage.length == 0 &&
        this.opmessage.length == 0 &&
        this.opDatemessage.length == 0 &&
        this.feNamemessage == 0 &&
        this.fcname.length != 0 &&
        this.crNo.length != 0 &&
        this.eNo.length != 0 &&
        this.opType.length != 0 &&
        this.fSDate.length != 0 &&
        this.fEDate.length != 0 &&
        this.opDate.length != 0
      ) {
        let crfixifo = {};
        crfixifo = {
          crNo: this.crNo,
          fcname: this.fcname,
          fedate: this.fEDate,
          fsdate: this.fSDate,
          fcremark: this.fcremark
        };
        let cropifo = {};
        cropifo = {
          crNo: this.crNo,
          eno: this.eNo,
          opDate: this.opDate,
          opType: this.opType
        };
        let fixcontact = {};
        fixcontact = {
          fcname: this.fcname,
          fcremark: this.fcremark
        };
        console.log(crfixifo);
        console.log(cropifo);
        console.log(fixcontact);

        const { data: res } = await this.$http.post({
          url: "crms/fixAdmin/insert",
          data: {
            crfixifo: crfixifo,
            cropifo: cropifo,
            fixcontact: fixcontact
          }
        });
        if (res.flag == true) {
          wx.showToast({
            title: "提交成功",
            icon: "none",
            duration: 1500
          });
          wx.navigateBack({
            delta: 1
          });
        }
        else{
          console.log(res);
         wx.showToast({
            title: res,
            icon: 'none',
            duration: 1500,
          });
          return 
        }
        return;
      } else {
        wx.showToast({
          title: "注意内容是否符合规则",
          icon: "none",
          duration: 1500
        });
      }
    }
  },
  onLoad: function(option) {
    this.eNo = wx.getStorageSync("eno");
    this.crNo = option.id;
  }
};
</script>

<style>
page {
  height: 100%;
  background: #f3f3f3;
}
.crfixifo,
.Cropifo,
.Lendcontact {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10rpx;
}
.van-field {
  width: 90% !important;
}
span {
  margin-bottom: 10rpx;
}
</style>